package com.keepers.repair.utils;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.support.v4.app.FragmentActivity;

/**
 * auth aboom
 * date 2018/12/2
 */
public class DialogUtils {

    /**
     * 是否可以显示Dialog
     *
     * @param context context
     * @return true 可以展示，false不能展示
     */
    public static boolean activityIsRunning(final Context context) {
        Activity activity = scanForActivity(context);
        if (activity == null) {
            return Boolean.FALSE;
        }
        if (activity.isFinishing()) {
            return Boolean.FALSE;
        }
        if (activity.isDestroyed()) {
            return Boolean.FALSE;
        }
        if (context instanceof FragmentActivity) {
            FragmentActivity fragmentActivity = (FragmentActivity) context;
            if (fragmentActivity.getSupportFragmentManager() != null && fragmentActivity.getSupportFragmentManager().isDestroyed()) {
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }


    /**
     * 返回activity
     */
    private static Activity scanForActivity(Context context) {
        if (context == null) {
            return null;
        }
        if (context instanceof Activity) {
            return (Activity) context;
        }
        if (context instanceof ContextWrapper) {
            return scanForActivity(((ContextWrapper) context).getBaseContext());
        }
        return null;
    }
}
